package cz.cvut.fel.nur.rcp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;
import cz.cvut.fel.nur.rcp.classes.Recipe;

public class Recepty extends Activity implements OnClickListener {

	GlobalClass appState;

	Button addRecipe, menuDown, menuUp;

	ImageView pantry, recipes, list, options, home, refresh;

	List<TextView> recipeViews;
	List<ImageView> removeRecipeViews;
	
	TableLayout recipeL;
	TableLayout removeRecipeL;
	
	int recipeHash;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recepty);
		super.onCreate(savedInstanceState);

		appState = ((GlobalClass) getApplicationContext());

		recipeL = (TableLayout) findViewById(R.id.recipesLayout);
		removeRecipeL = (TableLayout) findViewById(R.id.removeRecipeLayout);

		recipeViews = new ArrayList<TextView>();
		removeRecipeViews = new ArrayList<ImageView>();

//		for (Recipe r : appState.getRecipes()) {
//			TextView newR = new TextView(this);
//			newR.setText(r.getName());
//			newR.setTextSize(20);
//			newR.setGravity(0);
//			newR.setPadding(10, 10, 0, 10);
//			newR.setTextColor(-16777216);
//			newR.setId(r.getName().hashCode());
//			newR.setClickable(true);
//			newR.setOnClickListener((android.view.View.OnClickListener) this);
//			recipeL.addView(newR, 0);
//			
//			ImageView newRR = new ImageView(this);
//			newRR.setAdjustViewBounds(true);
//			newRR.setId((r.getName() + "Remove").hashCode());
//			newRR.setClickable(true);
//			newRR.setImageResource(R.drawable.minus);
//			newRR.setMaxHeight(65);
//			newRR.setMaxWidth(65);
//			newRR.setOnClickListener((android.view.View.OnClickListener) this);
//			removeRecipeL.addView(newRR, 0);
//
//			recipeViews.add(newR);
//			removeRecipeViews.add(newRR);
//		}

		addRecipe = (Button) findViewById(R.id.addRecipeButton);
		addRecipe.setOnClickListener((android.view.View.OnClickListener) this);
		appState = ((GlobalClass) getApplicationContext());

		menuDown = (Button) findViewById(R.id.menuDownButton);
		menuDown.setOnClickListener((android.view.View.OnClickListener) this);
		menuUp = (Button) findViewById(R.id.menuUpButton);
		menuUp.setOnClickListener((android.view.View.OnClickListener) this);

		pantry = (ImageView) findViewById(R.id.pantryButton);
		pantry.setOnClickListener((android.view.View.OnClickListener) this);
		recipes = (ImageView) findViewById(R.id.recipesButton);
		recipes.setOnClickListener((android.view.View.OnClickListener) this);
		list = (ImageView) findViewById(R.id.listButton);
		list.setOnClickListener((android.view.View.OnClickListener) this);
		options = (ImageView) findViewById(R.id.optionsButton);
		options.setOnClickListener((android.view.View.OnClickListener) this);
		home = (ImageView) findViewById(R.id.homeButton);
		home.setOnClickListener((android.view.View.OnClickListener) this);
		refresh = (ImageView) findViewById(R.id.refreshButton);
		refresh.setOnClickListener((android.view.View.OnClickListener) this);
		
		// visibility
		menuDown.setVisibility(View.GONE); // 0 is visible
		pantry.setVisibility(View.GONE);
		recipes.setVisibility(View.GONE);
		list.setVisibility(View.GONE);
		options.setVisibility(View.GONE);
		home.setVisibility(View.GONE);
	}

	public void onResume() {
		super.onResume();
		
		generateList();
	}
	
	private void generateList() {
		
		recipeL.removeAllViews();
		removeRecipeL.removeAllViews();

		recipeViews = new ArrayList<TextView>();
		removeRecipeViews = new ArrayList<ImageView>();

		for (Recipe r : appState.getRecipes()) {
			TextView newR = new TextView(this);
			newR.setText(r.getName());
			newR.setTextSize(20);
			newR.setGravity(0);
			newR.setPadding(10, 10, 0, 10);
			newR.setTextColor(-16777216);
			newR.setId(r.getName().hashCode());
			newR.setClickable(true);
			newR.setOnClickListener((android.view.View.OnClickListener) this);
			recipeL.addView(newR, 0);
			
			ImageView newRR = new ImageView(this);
			newRR.setAdjustViewBounds(true);
			newRR.setId((r.getName() + "Remove").hashCode());
			newRR.setClickable(true);
			newRR.setImageResource(R.drawable.minus);
			newRR.setMaxHeight(61);
			newRR.setMaxWidth(61);
			newRR.setOnClickListener((android.view.View.OnClickListener) this);
			removeRecipeL.addView(newRR, 0);

			recipeViews.add(newR);
			removeRecipeViews.add(newRR);
		}
	}
	
	public void onClick(View v) {
		/* pridani receptu */
		if (v.getId() == R.id.addToListButton) {
			startActivity(new Intent(this, ReceptDetail.class));
		}

		if (v.getId() == R.id.menuDownButton) {
			menuDown.setVisibility(View.GONE);
			pantry.setVisibility(View.GONE);
			recipes.setVisibility(View.GONE);
			list.setVisibility(View.GONE);
			options.setVisibility(View.GONE);
			home.setVisibility(View.GONE);
		}
		if (v.getId() == R.id.menuUpButton) {
			menuDown.setVisibility(0);
			pantry.setVisibility(0);
			recipes.setVisibility(0);
			list.setVisibility(0);
			options.setVisibility(0);
			home.setVisibility(0);
		}

		/* obrazky */
		if (v.getId() == R.id.pantryButton) {
			startActivity(new Intent(this, Spiz.class));
		}
		if (v.getId() == R.id.recipesButton) {
			startActivity(new Intent(this, Recepty.class));
		}
		if (v.getId() == R.id.listButton) {
			startActivity(new Intent(this, NakupniSeznam.class));
		}
		if (v.getId() == R.id.optionsButton) {
			startActivity(new Intent(this, Nastaveni.class));
		}
		if (v.getId() == R.id.homeButton) {
			startActivity(new Intent(this, Main.class));
		}

		/* klik na recept */
		for (TextView tv : recipeViews) {
			if (tv.getId() == v.getId()) {
				appState.currentRecipe = appState.getRecipe(tv.getId());
				startActivity(new Intent(this, ReceptDetail.class));
				break;
			}
		}

		/* odebirani receptu */
		int i = 0;
		for (ImageView iv : removeRecipeViews) {
			if (iv.getId() == v.getId()) {
//				iv.setVisibility(View.GONE);
//				recipeViews.get(i).setVisibility(View.GONE);
				recipeHash = recipeViews.get(i).getId();
				
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				
				builder.setMessage("Opravdu chcete odstranit recept?\n(" + 
						appState.getRecipe(recipeHash).getName() + ")");
				
				// Zamena positive/negative buttonu, aby potvrzeni bylo napravo
				builder.setPositiveButton("Ponechat", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			           }
			       });
				
				builder.setNegativeButton("Odstranit", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				        	   appState.removeRecipe(recipeHash);
				        	   generateList();
				           }
				       });
				
				AlertDialog dialog = builder.create();
				dialog.show();

				break;
			}
			i++;
		}
		
		if (v.getId() == R.id.addRecipeButton) {
			startActivity(new Intent(this, ReceptPridat.class));
		}
	}

}
